//
//  AddLoctionViewController.m
//  FKGW
//
//  Created by song ce on 2017/3/3.
//  Copyright © 2017年 song ce. All rights reserved.
//

#import "AddLoctionViewController.h"

@interface AddLoctionViewController ()
@property (weak, nonatomic) IBOutlet UITextField *receiveName;
@property (weak, nonatomic) IBOutlet UITextField *receivePhone;
@property (weak, nonatomic) IBOutlet UITextView *receiveAddress;
@property (weak, nonatomic) IBOutlet UIButton *codeBtn;
@property(nonatomic,copy)NSString *code;

@end

@implementation AddLoctionViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    if(!self.isAddVC){
        self.receiveName.text = _model.receiverName;
        self.receivePhone.text = _model.receiverPhone;
        self.receiveAddress.text = _model.receiverAddress;
//        self.codeBtn.hidden = YES;
    }
}

- (IBAction)commit:(id)sender {
    if(!_receiveName.text.length){
        [MBProgressHUD showTipMessageInWindow:@"请输入收货人名字"];
        return;
    }
    if(!_receivePhone.text.length) {
        [MBProgressHUD showTipMessageInWindow:@"请输入手机号码"];
        return;
    }
    if(![Tools isRightMobileNumber:_receivePhone.text]){
        [MBProgressHUD showTipMessageInWindow:@"手机号码格式不正确"];
        return;
    }
    if(!_receiveAddress.text.length){
        [MBProgressHUD showTipMessageInWindow:@"请输入收货地址"];
        return;
    }
    
    if(self.isAddVC){
        [self addLocation];
    }else{
        [self editeLoaction];
    }
}

-(void)addLocation{
    // 2.53增加收货地址
    NSDictionary *paraDic = @{
                              @"userId":USERID,
                              @"recName":self.receiveName.text,
                              @"phoneNum":self.receivePhone.text,
                              @"recAddress":self.receiveAddress.text
                              };
    __weak typeof( self) weakSelf= self;
    [Tools AFNetworkingRequestTypePostWithUrl:ADDRECTADDRESS paramters:paraDic  afterResultBlock:^(NSDictionary *resultDic) {
        DLog(@"增加收货地址接口---%@",resultDic);
        if([resultDic[@"flag"] isEqualToString:@"true"]){
            NSDictionary *dataDic = resultDic[@"data"];
            if([dataDic[@"status"] isEqualToString:@"true"]){
                [MBProgressHUD showTipMessageInView:@"添加成功"];
                if(weakSelf.isConfirmVcAddAddress){
                    [[NSNotificationCenter defaultCenter]postNotificationName:@"AddReceiveAddress" object:nil];
                }
                [weakSelf performSelector:@selector(popToSettingVc) withObject:nil afterDelay:1];
            }else{
                if(dataDic[@"errorString"]){
                    [MBProgressHUD showTipMessageInWindow:dataDic[@"errorString"]];
                }
            }
        }else{
            if(resultDic[@"errorString"]){
                [MBProgressHUD showTipMessageInWindow:resultDic[@"errorString"]];
            }
        }
    }];
}

-(void)editeLoaction{
    // 2.53编辑收货地址
    NSDictionary *paraDic = @{
                              @"userId":USERID,
                              @"recName":self.receiveName.text,
                              @"phoneNum":self.receivePhone.text,
                              @"recAddress":self.receiveAddress.text,
                              @"recAdrId":_model.recId
                              };
    __weak typeof( self) weakSelf= self;
    [Tools AFNetworkingRequestTypePostWithUrl:UPDATERECADR paramters:paraDic  afterResultBlock:^(NSDictionary *resultDic) {
        DLog(@"编辑收货地址接口---%@",resultDic);
        if([resultDic[@"flag"] isEqualToString:@"true"]){
            NSDictionary *dataDic = resultDic[@"data"];
            if([dataDic[@"status"] isEqualToString:@"true"]){
                [MBProgressHUD showTipMessageInWindow:@"修改成功"];
                if(weakSelf.isConfirmVc){
                    LocationModel *model = [[LocationModel alloc]init];
                    model.receiverName = self.receiveName.text;
                    model.receiverPhone = self.receivePhone.text;
                    model.receiverAddress = self.receiveAddress.text;
                    model.isDefault = _model.isDefault;
                     [[NSNotificationCenter defaultCenter]postNotificationName:@"changeReceiveAddress" object:model];
                }
                [weakSelf performSelector:@selector(popToSettingVc) withObject:nil afterDelay:1];
            }else{
                if(dataDic[@"errorString"]){
                    [MBProgressHUD showTipMessageInWindow:dataDic[@"errorString"]];
                }
            }
        }else{
            if(resultDic[@"errorString"]){
                [MBProgressHUD showTipMessageInWindow:resultDic[@"errorString"]];
            }
        }
    }];
}

-(void)popToSettingVc{
    [self.navigationController popViewControllerAnimated:YES];
}

@end
